package Demo;

import java.util.concurrent.Semaphore;

public class Demo01 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        Runnable runnable = () -> {
            try {
                System.out.println("申请资源");
                semaphore.acquire();
                Thread.sleep(2000);
                System.out.println("释放资源");
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(runnable);
            t.start();
        }

    }
}
